<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Anisotropic image segmentation by a gradient structure tensor</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d7/da8/tutorial_table_of_content_imgproc.html">Image Processing (imgproc module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Anisotropic image segmentation by a gradient structure tensor </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d1/dfd/tutorial_motion_deblur_filter.html">Motion Deblur Filter</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../d2/d0b/tutorial_periodic_noise_removing_filter.html">Periodic Noise Removing Filter</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Karpushin Vladislav </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<h2>Goal </h2>
<p>In this tutorial you will learn:</p>
<ul>
<li>what the gradient structure tensor is</li>
<li>how to estimate orientation and coherency of an anisotropic image by a gradient structure tensor</li>
<li>how to segment an anisotropic image with a single local orientation by a gradient structure tensor</li>
</ul>
<h2>Theory </h2>
<dl class="section note"><dt>Note</dt><dd>The explanation is based on the books <a class="el" href="../../d0/de3/citelist.html#CITEREF_jahne2000computer">[120]</a>, <a class="el" href="../../d0/de3/citelist.html#CITEREF_bigun2006vision">[22]</a> and <a class="el" href="../../d0/de3/citelist.html#CITEREF_van1995estimators">[258]</a>. Good physical explanation of a gradient structure tensor is given in <a class="el" href="../../d0/de3/citelist.html#CITEREF_yang1996structure">[282]</a>. Also, you can refer to a wikipedia page <a href="https://en.wikipedia.org/wiki/Structure_tensor">Structure tensor</a>. </dd>
<dd>
A anisotropic image on this page is a real world image.</dd></dl>
<h3>What is the gradient structure tensor?</h3>
<p>In mathematics, the gradient structure tensor (also referred to as the second-moment matrix, the second order moment tensor, the inertia tensor, etc.) is a matrix derived from the gradient of a function. It summarizes the predominant directions of the gradient in a specified neighborhood of a point, and the degree to which those directions are coherent (coherency). The gradient structure tensor is widely used in image processing and computer vision for 2D/3D image segmentation, motion detection, adaptive filtration, local image features detection, etc.</p>
<p>Important features of anisotropic images include orientation and coherency of a local anisotropy. In this paper we will show how to estimate orientation and coherency, and how to segment an anisotropic image with a single local orientation by a gradient structure tensor.</p>
<p>The gradient structure tensor of an image is a 2x2 symmetric matrix. Eigenvectors of the gradient structure tensor indicate local orientation, whereas eigenvalues give coherency (a measure of anisotropism).</p>
<p>The gradient structure tensor \(J\) of an image \(Z\) can be written as:</p>
<p class="formulaDsp">
\[J = \begin{bmatrix} J_{11} &amp; J_{12} \\ J_{12} &amp; J_{22} \end{bmatrix}\]
</p>
<p>where \(J_{11} = M[Z_{x}^{2}]\), \(J_{22} = M[Z_{y}^{2}]\), \(J_{12} = M[Z_{x}Z_{y}]\) - components of the tensor, \(M[]\) is a symbol of mathematical expectation (we can consider this operation as averaging in a window w), \(Z_{x}\) and \(Z_{y}\) are partial derivatives of an image \(Z\) with respect to \(x\) and \(y\).</p>
<p>The eigenvalues of the tensor can be found in the below formula: </p><p class="formulaDsp">
\[\lambda_{1,2} = \frac{1}{2} \left [ J_{11} + J_{22} \pm \sqrt{(J_{11} - J_{22})^{2} + 4J_{12}^{2}} \right ] \]
</p>
<p> where \(\lambda_1\) - largest eigenvalue, \(\lambda_2\) - smallest eigenvalue.</p>
<h3>How to estimate orientation and coherency of an anisotropic image by gradient structure tensor?</h3>
<p>The orientation of an anisotropic image: </p><p class="formulaDsp">
\[\alpha = 0.5arctg\frac{2J_{12}}{J_{22} - J_{11}}\]
</p>
<p>Coherency: </p><p class="formulaDsp">
\[C = \frac{\lambda_1 - \lambda_2}{\lambda_1 + \lambda_2}\]
</p>
<p>The coherency ranges from 0 to 1. For ideal local orientation ( \(\lambda_2\) = 0, \(\lambda_1\) &gt; 0) it is one, for an isotropic gray value structure ( \(\lambda_1\) = \(\lambda_2\) &gt; 0) it is zero.</p>
<h2>Source code </h2>
<p>You can find source code in the <code>samples/cpp/tutorial_code/ImgProc/anisotropic_image_segmentation/anisotropic_image_segmentation.cpp</code> of the OpenCV source code library.</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> calcGST(<span class="keyword">const</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; inputImg, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; imgCoherencyOut, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; imgOrientationOut, <span class="keywordtype">int</span> w);</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">    <span class="keywordtype">int</span> W = 52;             <span class="comment">// window size is WxW</span></div><div class="line">    <span class="keywordtype">double</span> C_Thr = 0.43;    <span class="comment">// threshold for coherency</span></div><div class="line">    <span class="keywordtype">int</span> LowThr = 35;        <span class="comment">// threshold1 for orientation, it ranges from 0 to 180</span></div><div class="line">    <span class="keywordtype">int</span> HighThr = 57;       <span class="comment">// threshold2 for orientation, it ranges from 0 to 180</span></div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> imgIn = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(<span class="stringliteral">&quot;input.jpg&quot;</span>, <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80ae29981cfc153d3b0cef5c0daeedd2125">IMREAD_GRAYSCALE</a>);</div><div class="line">    <span class="keywordflow">if</span> (imgIn.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>()) <span class="comment">//check whether the image is loaded or not</span></div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;ERROR : Image cannot be loaded..!!&quot;</span> &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> imgCoherency, imgOrientation;</div><div class="line">    calcGST(imgIn, imgCoherency, imgOrientation, W);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> imgCoherencyBin;</div><div class="line">    imgCoherencyBin = imgCoherency &gt; C_Thr;</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> imgOrientationBin;</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">inRange</a>(imgOrientation, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(LowThr), <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(HighThr), imgOrientationBin);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> imgBin;</div><div class="line">    imgBin = imgCoherencyBin &amp; imgOrientationBin;</div><div class="line"></div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>(imgCoherency, imgCoherency, 0, 255, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>(imgOrientation, imgOrientation, 0, 255, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>);</div><div class="line"></div><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(<span class="stringliteral">&quot;result.jpg&quot;</span>, 0.5*(imgIn + imgBin));</div><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(<span class="stringliteral">&quot;Coherency.jpg&quot;</span>, imgCoherency);</div><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(<span class="stringliteral">&quot;Orientation.jpg&quot;</span>, imgOrientation);</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"><span class="keywordtype">void</span> calcGST(<span class="keyword">const</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; inputImg, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; imgCoherencyOut, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; imgOrientationOut, <span class="keywordtype">int</span> w)</div><div class="line">{</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img;</div><div class="line">    inputImg.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#adf88c60c5b4980e05bb556080916978b">convertTo</a>(img, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>);</div><div class="line"></div><div class="line">    <span class="comment">// GST components calculation (start)</span></div><div class="line">    <span class="comment">// J =  (J11 J12; J12 J22) - GST</span></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> imgDiffX, imgDiffY, imgDiffXY;</div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">Sobel</a>(img, imgDiffX, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, 1, 0, 3);</div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">Sobel</a>(img, imgDiffY, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, 0, 1, 3);</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">multiply</a>(imgDiffX, imgDiffY, imgDiffXY);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> imgDiffXX, imgDiffYY;</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">multiply</a>(imgDiffX, imgDiffX, imgDiffXX);</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">multiply</a>(imgDiffY, imgDiffY, imgDiffYY);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> J11, J22, J12;      <span class="comment">// J11, J22 and J12 are GST components</span></div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">boxFilter</a>(imgDiffXX, J11, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(w, w));</div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">boxFilter</a>(imgDiffYY, J22, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(w, w));</div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">boxFilter</a>(imgDiffXY, J12, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(w, w));</div><div class="line">    <span class="comment">// GST components calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="comment">// eigenvalue calculation (start)</span></div><div class="line">    <span class="comment">// lambda1 = 0.5*(J11 + J22 + sqrt((J11-J22)^2 + 4*J12^2))</span></div><div class="line">    <span class="comment">// lambda2 = 0.5*(J11 + J22 - sqrt((J11-J22)^2 + 4*J12^2))</span></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> tmp1, tmp2, tmp3, tmp4;</div><div class="line">    tmp1 = J11 + J22;</div><div class="line">    tmp2 = J11 - J22;</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">multiply</a>(tmp2, tmp2, tmp2);</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">multiply</a>(J12, J12, tmp3);</div><div class="line">    <a class="code" href="../../d0/de1/group__core.html#ga9070b6a3f093dd952d973819b06f4906">sqrt</a>(tmp2 + 4.0 * tmp3, tmp4);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> lambda1, lambda2;</div><div class="line">    lambda1 = tmp1 + tmp4;</div><div class="line">    lambda1 = 0.5*lambda1;      <span class="comment">// biggest eigenvalue</span></div><div class="line">    lambda2 = tmp1 - tmp4;</div><div class="line">    lambda2 = 0.5*lambda2;      <span class="comment">// smallest eigenvalue</span></div><div class="line">    <span class="comment">// eigenvalue calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="comment">// Coherency calculation (start)</span></div><div class="line">    <span class="comment">// Coherency = (lambda1 - lambda2)/(lambda1 + lambda2)) - measure of anisotropism</span></div><div class="line">    <span class="comment">// Coherency is anisotropy degree (consistency of local orientation)</span></div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga6db555d30115642fedae0cda05604874">divide</a>(lambda1 - lambda2, lambda1 + lambda2, imgCoherencyOut);</div><div class="line">    <span class="comment">// Coherency calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="comment">// orientation angle calculation (start)</span></div><div class="line">    <span class="comment">// tan(2*Alpha) = 2*J12/(J22 - J11)</span></div><div class="line">    <span class="comment">// Alpha = 0.5 atan2(2*J12/(J22 - J11))</span></div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga9db9ca9b4d81c3bde5677b8f64dc0137">phase</a>(J22 - J11, 2.0*J12, imgOrientationOut, <span class="keyword">true</span>);</div><div class="line">    imgOrientationOut = 0.5*imgOrientationOut;</div><div class="line">    <span class="comment">// orientation angle calculation (stop)</span></div><div class="line">}</div></div><!-- fragment -->  </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"></div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"><span class="keyword">import</span> argparse</div><div class="line"></div><div class="line">W = 52          <span class="comment"># window size is WxW</span></div><div class="line">C_Thr = 0.43    <span class="comment"># threshold for coherency</span></div><div class="line">LowThr = 35     <span class="comment"># threshold1 for orientation, it ranges from 0 to 180</span></div><div class="line">HighThr = 57    <span class="comment"># threshold2 for orientation, it ranges from 0 to 180</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">def </span>calcGST(inputIMG, w):</div><div class="line"></div><div class="line">    img = inputIMG.astype(np.float32)</div><div class="line"></div><div class="line">    <span class="comment"># GST components calculation (start)</span></div><div class="line">    <span class="comment"># J =  (J11 J12; J12 J22) - GST</span></div><div class="line">    imgDiffX = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">cv.Sobel</a>(img, cv.CV_32F, 1, 0, 3)</div><div class="line">    imgDiffY = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">cv.Sobel</a>(img, cv.CV_32F, 0, 1, 3)</div><div class="line">    imgDiffXY = <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">cv.multiply</a>(imgDiffX, imgDiffY)</div><div class="line">    </div><div class="line"></div><div class="line">    imgDiffXX = <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">cv.multiply</a>(imgDiffX, imgDiffX)</div><div class="line">    imgDiffYY = <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">cv.multiply</a>(imgDiffY, imgDiffY)</div><div class="line"></div><div class="line">    J11 = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">cv.boxFilter</a>(imgDiffXX, cv.CV_32F, (w,w))</div><div class="line">    J22 = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">cv.boxFilter</a>(imgDiffYY, cv.CV_32F, (w,w))</div><div class="line">    J12 = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">cv.boxFilter</a>(imgDiffXY, cv.CV_32F, (w,w))</div><div class="line">    <span class="comment"># GST components calculations (stop)</span></div><div class="line"></div><div class="line">    <span class="comment"># eigenvalue calculation (start)</span></div><div class="line">    <span class="comment"># lambda1 = 0.5*(J11 + J22 + sqrt((J11-J22)^2 + 4*J12^2))</span></div><div class="line">    <span class="comment"># lambda2 = 0.5*(J11 + J22 - sqrt((J11-J22)^2 + 4*J12^2))</span></div><div class="line">    tmp1 = J11 + J22</div><div class="line">    tmp2 = J11 - J22</div><div class="line">    tmp2 = <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">cv.multiply</a>(tmp2, tmp2)</div><div class="line">    tmp3 = <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">cv.multiply</a>(J12, J12)</div><div class="line">    tmp4 = np.sqrt(tmp2 + 4.0 * tmp3)</div><div class="line"></div><div class="line">    lambda1 = 0.5*(tmp1 + tmp4)    <span class="comment"># biggest eigenvalue</span></div><div class="line">    lambda2 = 0.5*(tmp1 - tmp4)    <span class="comment"># smallest eigenvalue</span></div><div class="line">    <span class="comment"># eigenvalue calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="comment"># Coherency calculation (start)</span></div><div class="line">    <span class="comment"># Coherency = (lambda1 - lambda2)/(lambda1 + lambda2)) - measure of anisotropism</span></div><div class="line">    <span class="comment"># Coherency is anisotropy degree (consistency of local orientation)</span></div><div class="line">    imgCoherencyOut = <a class="code" href="../../d2/de8/group__core__array.html#ga1f96b569cac4c286642b34eff098138e">cv.divide</a>(lambda1 - lambda2, lambda1 + lambda2)</div><div class="line">    <span class="comment"># Coherency calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="comment"># orientation angle calculation (start)</span></div><div class="line">    <span class="comment"># tan(2*Alpha) = 2*J12/(J22 - J11)</span></div><div class="line">    <span class="comment"># Alpha = 0.5 atan2(2*J12/(J22 - J11))</span></div><div class="line">    imgOrientationOut = <a class="code" href="../../d2/de8/group__core__array.html#ga9db9ca9b4d81c3bde5677b8f64dc0137">cv.phase</a>(J22 - J11, 2.0 * J12, angleInDegrees = <span class="keyword">True</span>)</div><div class="line">    imgOrientationOut = 0.5 * imgOrientationOut</div><div class="line">    <span class="comment"># orientation angle calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> imgCoherencyOut, imgOrientationOut</div><div class="line"></div><div class="line"></div><div class="line">parser = argparse.ArgumentParser(description=<span class="stringliteral">&#39;Code for Anisotropic image segmentation tutorial.&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;-i&#39;</span>, <span class="stringliteral">&#39;--input&#39;</span>, help=<span class="stringliteral">&#39;Path to input image.&#39;</span>, required=<span class="keyword">True</span>)</div><div class="line">args = parser.parse_args()</div><div class="line"></div><div class="line">imgIn = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(args.input, cv.IMREAD_GRAYSCALE)</div><div class="line"><span class="keywordflow">if</span> imgIn <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">    <a class="code" href="../../df/d57/namespacecv_1_1dnn.html#a701210a0203f2786cbfd04b2bd56da47">print</a>(<span class="stringliteral">&#39;Could not open or find the image: {}&#39;</span>.format(args.input))</div><div class="line">    exit(0)</div><div class="line"></div><div class="line"></div><div class="line">imgCoherency, imgOrientation = calcGST(imgIn, W)</div><div class="line"></div><div class="line"></div><div class="line">_, imgCoherencyBin = <a class="code" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv.threshold</a>(imgCoherency, C_Thr, 255, cv.THRESH_BINARY)</div><div class="line">_, imgOrientationBin = <a class="code" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv.threshold</a>(imgOrientation, LowThr, HighThr, cv.THRESH_BINARY)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">imgBin = <a class="code" href="../../d2/de8/group__core__array.html#ga60b4d04b251ba5eb1392c34425497e14">cv.bitwise_and</a>(imgCoherencyBin, imgOrientationBin)</div><div class="line"></div><div class="line"></div><div class="line">imgCoherency = <a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(imgCoherency, <span class="keywordtype">None</span>, alpha=0, beta=1, norm_type=cv.NORM_MINMAX, dtype=cv.CV_32F)</div><div class="line">imgOrientation = <a class="code" href="../../d2/de8/group__core__array.html#ga7bcf47a1df78cf575162e0aed44960cb">cv.normalize</a>(imgOrientation, <span class="keywordtype">None</span>, alpha=0, beta=1, norm_type=cv.NORM_MINMAX, dtype=cv.CV_32F)</div><div class="line"></div><div class="line"><a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;result.jpg&#39;</span>, np.uint8(0.5*(imgIn + imgBin)))</div><div class="line"><a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;Coherency.jpg&#39;</span>, imgCoherency)</div><div class="line"><a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;Orientation.jpg&#39;</span>, imgOrientation)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(0)</div><div class="line"></div></div><!-- fragment -->  </div> <h2>Explanation </h2>
<p>An anisotropic image segmentation algorithm consists of a gradient structure tensor calculation, an orientation calculation, a coherency calculation and an orientation and coherency thresholding:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat imgCoherency, imgOrientation;</div><div class="line">    calcGST(imgIn, imgCoherency, imgOrientation, W);</div><div class="line"></div><div class="line">    Mat imgCoherencyBin;</div><div class="line">    imgCoherencyBin = imgCoherency &gt; C_Thr;</div><div class="line">    Mat imgOrientationBin;</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">inRange</a>(imgOrientation, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(LowThr), <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(HighThr), imgOrientationBin);</div><div class="line"></div><div class="line">    Mat imgBin;</div><div class="line">    imgBin = imgCoherencyBin &amp; imgOrientationBin;</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">imgCoherency, imgOrientation = calcGST(imgIn, W)</div><div class="line"></div><div class="line"></div><div class="line">_, imgCoherencyBin = <a class="code" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv.threshold</a>(imgCoherency, C_Thr, 255, cv.THRESH_BINARY)</div><div class="line">_, imgOrientationBin = <a class="code" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv.threshold</a>(imgOrientation, LowThr, HighThr, cv.THRESH_BINARY)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">imgBin = <a class="code" href="../../d2/de8/group__core__array.html#ga60b4d04b251ba5eb1392c34425497e14">cv.bitwise_and</a>(imgCoherencyBin, imgOrientationBin)</div><div class="line"></div></div><!-- fragment --> </div> <p>A function calcGST() calculates orientation and coherency by using a gradient structure tensor. An input parameter w defines a window size:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line"><span class="keywordtype">void</span> calcGST(<span class="keyword">const</span> Mat&amp; inputImg, Mat&amp; imgCoherencyOut, Mat&amp; imgOrientationOut, <span class="keywordtype">int</span> w)</div><div class="line">{</div><div class="line">    Mat img;</div><div class="line">    inputImg.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#adf88c60c5b4980e05bb556080916978b">convertTo</a>(img, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>);</div><div class="line"></div><div class="line">    <span class="comment">// GST components calculation (start)</span></div><div class="line">    <span class="comment">// J =  (J11 J12; J12 J22) - GST</span></div><div class="line">    Mat imgDiffX, imgDiffY, imgDiffXY;</div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">Sobel</a>(img, imgDiffX, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, 1, 0, 3);</div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">Sobel</a>(img, imgDiffY, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, 0, 1, 3);</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">multiply</a>(imgDiffX, imgDiffY, imgDiffXY);</div><div class="line"></div><div class="line">    Mat imgDiffXX, imgDiffYY;</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">multiply</a>(imgDiffX, imgDiffX, imgDiffXX);</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">multiply</a>(imgDiffY, imgDiffY, imgDiffYY);</div><div class="line"></div><div class="line">    Mat J11, J22, J12;      <span class="comment">// J11, J22 and J12 are GST components</span></div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">boxFilter</a>(imgDiffXX, J11, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(w, w));</div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">boxFilter</a>(imgDiffYY, J22, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(w, w));</div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">boxFilter</a>(imgDiffXY, J12, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(w, w));</div><div class="line">    <span class="comment">// GST components calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="comment">// eigenvalue calculation (start)</span></div><div class="line">    <span class="comment">// lambda1 = 0.5*(J11 + J22 + sqrt((J11-J22)^2 + 4*J12^2))</span></div><div class="line">    <span class="comment">// lambda2 = 0.5*(J11 + J22 - sqrt((J11-J22)^2 + 4*J12^2))</span></div><div class="line">    Mat tmp1, tmp2, tmp3, tmp4;</div><div class="line">    tmp1 = J11 + J22;</div><div class="line">    tmp2 = J11 - J22;</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">multiply</a>(tmp2, tmp2, tmp2);</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">multiply</a>(J12, J12, tmp3);</div><div class="line">    <a class="code" href="../../d0/de1/group__core.html#ga9070b6a3f093dd952d973819b06f4906">sqrt</a>(tmp2 + 4.0 * tmp3, tmp4);</div><div class="line"></div><div class="line">    Mat lambda1, lambda2;</div><div class="line">    lambda1 = tmp1 + tmp4;</div><div class="line">    lambda1 = 0.5*lambda1;      <span class="comment">// biggest eigenvalue</span></div><div class="line">    lambda2 = tmp1 - tmp4;</div><div class="line">    lambda2 = 0.5*lambda2;      <span class="comment">// smallest eigenvalue</span></div><div class="line">    <span class="comment">// eigenvalue calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="comment">// Coherency calculation (start)</span></div><div class="line">    <span class="comment">// Coherency = (lambda1 - lambda2)/(lambda1 + lambda2)) - measure of anisotropism</span></div><div class="line">    <span class="comment">// Coherency is anisotropy degree (consistency of local orientation)</span></div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga6db555d30115642fedae0cda05604874">divide</a>(lambda1 - lambda2, lambda1 + lambda2, imgCoherencyOut);</div><div class="line">    <span class="comment">// Coherency calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="comment">// orientation angle calculation (start)</span></div><div class="line">    <span class="comment">// tan(2*Alpha) = 2*J12/(J22 - J11)</span></div><div class="line">    <span class="comment">// Alpha = 0.5 atan2(2*J12/(J22 - J11))</span></div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga9db9ca9b4d81c3bde5677b8f64dc0137">phase</a>(J22 - J11, 2.0*J12, imgOrientationOut, <span class="keyword">true</span>);</div><div class="line">    imgOrientationOut = 0.5*imgOrientationOut;</div><div class="line">    <span class="comment">// orientation angle calculation (stop)</span></div><div class="line">}</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"></div><div class="line"><span class="keyword">def </span>calcGST(inputIMG, w):</div><div class="line"></div><div class="line">    img = inputIMG.astype(np.float32)</div><div class="line"></div><div class="line">    <span class="comment"># GST components calculation (start)</span></div><div class="line">    <span class="comment"># J =  (J11 J12; J12 J22) - GST</span></div><div class="line">    imgDiffX = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">cv.Sobel</a>(img, cv.CV_32F, 1, 0, 3)</div><div class="line">    imgDiffY = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">cv.Sobel</a>(img, cv.CV_32F, 0, 1, 3)</div><div class="line">    imgDiffXY = <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">cv.multiply</a>(imgDiffX, imgDiffY)</div><div class="line">    </div><div class="line"></div><div class="line">    imgDiffXX = <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">cv.multiply</a>(imgDiffX, imgDiffX)</div><div class="line">    imgDiffYY = <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">cv.multiply</a>(imgDiffY, imgDiffY)</div><div class="line"></div><div class="line">    J11 = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">cv.boxFilter</a>(imgDiffXX, cv.CV_32F, (w,w))</div><div class="line">    J22 = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">cv.boxFilter</a>(imgDiffYY, cv.CV_32F, (w,w))</div><div class="line">    J12 = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3">cv.boxFilter</a>(imgDiffXY, cv.CV_32F, (w,w))</div><div class="line">    <span class="comment"># GST components calculations (stop)</span></div><div class="line"></div><div class="line">    <span class="comment"># eigenvalue calculation (start)</span></div><div class="line">    <span class="comment"># lambda1 = 0.5*(J11 + J22 + sqrt((J11-J22)^2 + 4*J12^2))</span></div><div class="line">    <span class="comment"># lambda2 = 0.5*(J11 + J22 - sqrt((J11-J22)^2 + 4*J12^2))</span></div><div class="line">    tmp1 = J11 + J22</div><div class="line">    tmp2 = J11 - J22</div><div class="line">    tmp2 = <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">cv.multiply</a>(tmp2, tmp2)</div><div class="line">    tmp3 = <a class="code" href="../../d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f">cv.multiply</a>(J12, J12)</div><div class="line">    tmp4 = np.sqrt(tmp2 + 4.0 * tmp3)</div><div class="line"></div><div class="line">    lambda1 = 0.5*(tmp1 + tmp4)    <span class="comment"># biggest eigenvalue</span></div><div class="line">    lambda2 = 0.5*(tmp1 - tmp4)    <span class="comment"># smallest eigenvalue</span></div><div class="line">    <span class="comment"># eigenvalue calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="comment"># Coherency calculation (start)</span></div><div class="line">    <span class="comment"># Coherency = (lambda1 - lambda2)/(lambda1 + lambda2)) - measure of anisotropism</span></div><div class="line">    <span class="comment"># Coherency is anisotropy degree (consistency of local orientation)</span></div><div class="line">    imgCoherencyOut = <a class="code" href="../../d2/de8/group__core__array.html#ga1f96b569cac4c286642b34eff098138e">cv.divide</a>(lambda1 - lambda2, lambda1 + lambda2)</div><div class="line">    <span class="comment"># Coherency calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="comment"># orientation angle calculation (start)</span></div><div class="line">    <span class="comment"># tan(2*Alpha) = 2*J12/(J22 - J11)</span></div><div class="line">    <span class="comment"># Alpha = 0.5 atan2(2*J12/(J22 - J11))</span></div><div class="line">    imgOrientationOut = <a class="code" href="../../d2/de8/group__core__array.html#ga9db9ca9b4d81c3bde5677b8f64dc0137">cv.phase</a>(J22 - J11, 2.0 * J12, angleInDegrees = <span class="keyword">True</span>)</div><div class="line">    imgOrientationOut = 0.5 * imgOrientationOut</div><div class="line">    <span class="comment"># orientation angle calculation (stop)</span></div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> imgCoherencyOut, imgOrientationOut</div></div><!-- fragment --> </div> <p>The below code applies a thresholds LowThr and HighThr to image orientation and a threshold C_Thr to image coherency calculated by the previous function. LowThr and HighThr define orientation range:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat imgCoherencyBin;</div><div class="line">    imgCoherencyBin = imgCoherency &gt; C_Thr;</div><div class="line">    Mat imgOrientationBin;</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">inRange</a>(imgOrientation, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(LowThr), <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(HighThr), imgOrientationBin);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">_, imgCoherencyBin = <a class="code" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv.threshold</a>(imgCoherency, C_Thr, 255, cv.THRESH_BINARY)</div><div class="line">_, imgOrientationBin = <a class="code" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv.threshold</a>(imgOrientation, LowThr, HighThr, cv.THRESH_BINARY)</div></div><!-- fragment --> </div> <p>And finally we combine thresholding results:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    Mat imgBin;</div><div class="line">    imgBin = imgCoherencyBin &amp; imgOrientationBin;</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">imgBin = <a class="code" href="../../d2/de8/group__core__array.html#ga60b4d04b251ba5eb1392c34425497e14">cv.bitwise_and</a>(imgCoherencyBin, imgOrientationBin)</div></div><!-- fragment --> </div> <h2>Result </h2>
<p>Below you can see the real anisotropic image with single direction: </p><div class="image">
<img src="../../gst_input.jpg" alt="gst_input.jpg"/>
<div class="caption">
Anisotropic image with the single direction</div></div>
<p> Below you can see the orientation and coherency of the anisotropic image: </p><div class="image">
<img src="../../gst_orientation.jpg" alt="gst_orientation.jpg"/>
<div class="caption">
Orientation</div></div>
<div class="image">
<img src="../../gst_coherency.jpg" alt="gst_coherency.jpg"/>
<div class="caption">
Coherency</div></div>
<p> Below you can see the segmentation result: </p><div class="image">
<img src="../../gst_result.jpg" alt="gst_result.jpg"/>
<div class="caption">
Segmentation result</div></div>
<p> The result has been computed with w = 52, C_Thr = 0.43, LowThr = 35, HighThr = 57. We can see that the algorithm selected only the areas with one single direction.</p>
<h2>References </h2>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Structure_tensor">Structure tensor</a> - structure tensor description on the wikipedia </li>
</ul>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:35 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
